<?php

require_once 'constantes.php';
require_once 'DaoConn.class.php';

class DaoFilms extends DaoConn {
	
	function getFilmById($id) {
		$this->open();
		try {
			$sql = "SELECT * FROM ".FILMS." WHERE ".FILMS_ID." =:id;";
			$req = $this->bdd->prepare($sql);			
			$req->execute(array('id'=>$id));
			//$result = $req->fetch(PDO::FETCH_LAZY);
			$result = $req->fetch(PDO::FETCH_OBJ);
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
		return $result;
	}
		
	
	function getFilmAll() {
		$this->open();
		try {
			$sql = "SELECT * FROM ".FILMS.";";
			$req = $this->bdd->prepare($sql);
			$req->execute();
			$tab = array();			
			while($result = $req->fetch(PDO::FETCH_OBJ)){
				array_push($tab, $result);				
			}
			$req->closeCursor();
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
		return $tab;
	}
	/*
	*@arg : $idClient 
	*/
	function getFilmReserverByIdclient($idClient) {
		$this->open();
		try {
			$sql = "SELECT *, CURRENT_TIMESTAMP currenttime FROM ".FILMS." WHERE ".FILMS_CL_ID."=:id AND ".FILMS_STATUS."='".RESERVER."';";
			$req = $this->bdd->prepare($sql);
			$req->execute(array('id'=>$idClient));
			$tab = array();			
			while($result = $req->fetch(PDO::FETCH_OBJ)){
				array_push($tab, $result);				
			}
			$req->closeCursor();
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
		return $tab;
	}
	
	/*
	 * @arg $idClient, id du film
	* remonte tous les films louer par le client idClient
	*/
	function getFilmLouerByIdclient($idClient){
		$this->open();
		try {
			$sql = "SELECT *, CURRENT_TIMESTAMP currenttime FROM ".FILMS." WHERE ".FILMS_CL_ID."=:id AND ".FILMS_STATUS."='".LOCATION."';";
			$req = $this->bdd->prepare($sql);
			$req->execute(array('id'=>$idClient));
			$tab = array();
			while($result = $req->fetch(PDO::FETCH_OBJ)){
				array_push($tab, $result);
			}
			$req->closeCursor();
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
		return $tab;
	}
	
	function getFilmLouerByIdclientIdFilm($tab,$idClient){
		$this->open();
		try {
			$az = " ( ";
			foreach ($tab as $value){
				$az = $az.$value.' ,';
			}
			$az = substr($az, 0, -1);
			$az = $az." ) ";
				
			$sql = "SELECT *, CURRENT_TIMESTAMP currenttime FROM ".FILMS." WHERE ".FILMS_CL_ID."=:id AND ".FILMS_STATUS."='".LOCATION."' AND f_id IN ".$az.";";
			$req = $this->bdd->prepare($sql);
			$req->execute(array('id'=>$idClient));
			$tab = array();
			while($result = $req->fetch(PDO::FETCH_OBJ)){
				array_push($tab, $result);
			}
			$req->closeCursor();
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
		return $tab;
	}
	
	
	
	
	/*
	*@arg : tableau $tabIdFilm 
	*/
	function setDebutLocation($tabIdFilm){
		$this->open();
		try {
			foreach($tabIdFilm as $value){
				$sql = "UPDATE ".FILMS." SET ".FILMS_DEB_LOC." = CURRENT_TIMESTAMP , ".FILMS_STATUS." = '".LOCATION."' WHERE ".FILMS_ID." =:id ;";
				$req = $this->bdd->prepare($sql);
				$req->execute(array('id'=>$value));				
			}
		} catch (Exception $e) {
				die('ERREUR SQL : '.$e->getMessage());
		}	
		$this->close();
	}
	/*
	 * on pourrait faire un seul update....mais bon
	 */
	function setDVDADispo($tabIdFilm){
		$this->open();
		try {
			foreach($tabIdFilm as $value){
				
				$sql = "UPDATE ".FILMS." SET ".FILMS_STATUS." = '".DISPONIBLE."' WHERE ".FILMS_ID." =:id ;";
				$req = $this->bdd->prepare($sql);
				$req->execute(array('id'=>$value));
			}
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
	}
	
	/*
	*@arg tableau tabIdFilm
	*/
	//retourner un tableau associatif ??
// 	function isLocationPossible($tabIdFilm){
// 		$this->open();
// 		try {
// 			foreach($tabIdFilm as $value){
// 				$sql = "SELECT ;";
// 				$req = $this->bdd->prepare($sql);
// 				$req->execute(array('id'=>$value));				
// 			}
// 		} catch (Exception $e) {
// 				die('ERREUR SQL : '.$e->getMessage());
// 		}	
// 		$this->close();
// 	}
	
	
	
}